package atm;

import java.util.Scanner;

public class ATM05 {
    public static Scanner sc = new Scanner(System.in);
    private int index = -1;
    private User[] users = {
            new User("zyd1", "1", 1000),
            new User("zyd2", "2", 2000),
            new User("zyd3", "3", 3000),
            new User("zyd4", "4", 4000),
            new User("zyd5", "5", 5000)
    };

    public void setindex(int index) {
        this.index = index;
    }

    public static void process() {
        ATM05 atm=new ATM05();
        System.out.println("欢迎使用雨滴银行");
        login(atm);
        menu(atm);
    }

    /**
     * 登录
     */
    public static void login(ATM05 atm) {
        for (int i = 0; i < 3; i++) {
            // 登录判断
            System.out.println("请输入账号：");
            String account = sc.next();
            System.out.println("请输入密码：");
            String pwd = sc.next();
            boolean isLogin = false;
            for (int j = 0; j < atm.users.length; j++) {
                if (account.equals(atm.users[j].getName()) && pwd.equals(atm.users[j].getWord())){
                    System.out.println("登录成功");
                    isLogin = true;
                    atm.setindex(j);
                    break;
                }
            }
            if (isLogin) {
                break;
            } else {
                System.out.println("登录失败，还剩" + (2 - i) + "次");
                if (i == 2) {
                    System.out.println("卡已被吞");
                    System.exit(0);
                }
            }


        }


    }

    /**
     * 菜单
     */
    public static void menu(ATM05 atm) {
        while (true) {
            // 如果登录成功，进入菜单页面
            System.out.println("请选择：1-存款 2-取款 3-查询 4-转账 5-修改密码 6-切换账号 7-退出");
            int input = sc.nextInt();
            if (input == 1) {
                save(atm);
            }
            if (input == 2) {
                get(atm);
            }
            if (input == 3) {
                search(atm);
            }
            if (input == 4) {
                transfer(atm);
            }
            if (input == 5) {
                changePwd(atm);

            }
            if (input == 6) {
                login(atm);

            }
            if (input == 7) {

                System.out.println("再见");
                System.exit(0);
            }
        }
    }

    /**
     * 存款
     */
    public static void save(ATM05 atm) {
        System.out.println("请输入存的金额：");
        double blance = sc.nextDouble();
        atm.users[atm.index].setBalance(Double.parseDouble(atm.users[atm.index].getBalance() + blance + ""));
        System.out.println("存款成功！");
    }

    /**
     * 取款
     */
    public static void get(ATM05 atm) {
        System.out.println("请输入取款的金额：");
        double blance = sc.nextDouble();
        if (blance <= atm.users[atm.index].getBalance() && blance > 0){
            atm.users[atm.index].setBalance(atm.users[atm.index].getBalance() - blance);
            System.out.println("取款成功！");
        }else {
            System.out.println("取款失败");
        }
        }


    /**
     * 查询余额
     */
    public static void search(ATM05 atm) {
        System.out.println("您的余额为：" + atm.users[atm.index].getBalance());

    }

    /**
     * 转账
     */
    public static void transfer(ATM05 atm) {
        while (true) {
            System.out.println("请输入对方账号：");
            String otherAccount = sc.next();
            // 不能转账给自己
            if (otherAccount.equals(atm.users[atm.index].getName())) {
                System.out.println("不能转给自己");
                continue;
            }
            // 判断对方账号是否存在
            boolean isExist = false;
            int otherIndex = 0;
            for (int i = 0; i < atm.users.length; i++) {
                if (otherAccount.equals(atm.users[i].getName())) {
                    isExist = true;
                    otherIndex = i;
                    break;
                }
            }
            if (isExist) {
                System.out.println("请输入转账金额：");
                double blance = sc.nextDouble();
                if (blance <= atm.users[atm.index].getBalance() && blance > 0) {
                    atm.users[atm.index].setBalance(atm.users[atm.index].getBalance() - blance);
                    atm.users[otherIndex].setBalance(atm.users[otherIndex].getBalance() + blance);
                    System.out.println("转账成功");
                    break;
                } else {
                    System.out.println("转账失败，请检查输入金额或余额");
                }

            } else {
                System.out.println("未查找到相关账户");
            }
        }
    }



    /**
     * 修改密码
     * @return
     */
    public static void changePwd(ATM05 atm){
        boolean isExisc = false;
        String newPwd1 = "";
        String newPwd2 = "";
        System.out.println("请输入原密码：");
        String pwd = sc.next();
        if (atm.users[atm.index].getWord().equals(pwd)){
            isExisc = true;
        }else {
            System.out.println("密码错误");
            return;
        }
        if (isExisc){
            System.out.println("请输入新密码");
            newPwd1 = sc.next();
            System.out.println("再次输入新密码");
            newPwd2 = sc.next();
            if (newPwd1.equals(newPwd2)){
                System.out.println("密码修改成功");
                atm.users[atm.index].setWord(newPwd1);
            }else {
                System.out.println("新密码不相同，修改失败");
                return;
            }
        }
    }


    public static void main(String[] args) {
        process();
    }
}

